<?php
/**
 * @purpose: 对“通知”类型中子类型为14(@user)的编码与解码文件
 * @author: Mandel Woo <wuzhijian@snda.com>

 * @memo: 参阅母板文件
 */
require_once(dirname(__FILE__)."/../../../models/user/UCModUserInfo.php");


function msg_notice_type14_encoder() {
	$fields = array('s_id','comment','ori','t_id','from');


	foreach($fields as $k => $v) {
		eval("\${$v}=func_get_arg($k);");
	}

	if (isset($ori['content'])) {
		$ori['content'] = utf_substr($ori['content'], 150);
	}


	return serialize(array(
				's_id'   =>$s_id,
				'comment'=>utf_substr($comment, 180),
				'ori'    =>$ori,
				't_id'   =>$t_id,
				'from'   =>$from,
			));
}

function msg_notice_type14_decoder() {
	$fields = array("id","receiverid","content","createTime","changeFlag","fmt");

	foreach($fields as $k => $v) {
		eval("\${$v}=func_get_arg($k);");
	}


	$data           = unserialize($content);
	$sender_id      = $data['s_id'];
	$twitter_id     = $data['t_id'];
	$comment        = codeToSmile(nl2br(htmlspecialchars($data['comment'])));
	$from           = $data['from'];
	if (empty($data['ori'])) {
		$has_ori = false;
	} else {
		$has_ori = true;
		$ori_author     = $data['ori']['author'];
		$ori_content    = codeToSmile(nl2br(htmlspecialchars($data['ori']['content'])));
		$ori_twitter_id = $data['ori']['t_id'];
		$ori_n_com      = $data['ori']['n_com'];
		$ori_n_copy     = $data['ori']['n_copy'];
	}


	$users_infos = UCModUserInfo::getUserInfos(array($sender_id,$ori_author), array(USERINFO_NICKNAME,USERINFO_AVATAR_45));

	$sender_nick = htmlspecialchars($users_infos[$sender_id][USERINFO_NICKNAME]);
	$sender_nick == '' && $sender_nick = $sender_id;
	$sender = "<a href=\"".get_user_home_url($sender_id)."\" target='_blank'>{$sender_nick}</a>";

	$ori_author_nick = htmlspecialchars($users_infos[$ori_author][USERINFO_NICKNAME]);
	$ori_author_nick == '' && $ori_author_nick = $ori_author;
	$ori = "<a href=\"".get_user_home_url($ori_author)."\" target='_blank'>{$ori_author_nick}</a>";


	if ($fmt == 'line') {
		return sprintf("%s的推它@了你", $sender);

	} else if ($fmt == 'box') {
		$sender_photo = sprintf("<a href=\"%s\" target=\"_blank\"><img src=\"%s\" /></a>", get_user_home_url($sender_id), $users_infos[$sender_id][USERINFO_AVATAR_45]);

		$html = <<<HTML
<table cellspacing="10">
<tr>
	<td colspan="2">
		{$sender}@了你
	</td>
</tr>
<tr>
	<td rowspan="2" valign="top"><div style="padding:10px">{$sender_photo}</div></td>
	<td>
		{$sender}：{$comment}<br />
HTML;

		if ($has_ori) {
			$html .= <<<HTML
		<div style="background-color:#DEEFF7;padding:15px;">
			@{$ori_author_nick}：{$ori_content}<br /><br />
			<div style="font-size:12px;">
				<a href="/tuita/viewdetail/mood_id/{$ori_twitter_id}/user_id/{$ori_author}" target="_blank">原文评论({$ori_n_com})</a>
				&nbsp;&nbsp;
				<a href="/tuita/viewdetail/mood_id/{$ori_twitter_id}/user_id/{$ori_author}" target="_blank">原文转推({$ori_n_copy})</a>
			</div>
		</div>
HTML;
		}

		$html .= <<<HTML
	</td>
</tr>
<tr>
	<td>
		<div style="font-size:12px;margin-top:12px;">
			<a href="/tuita/viewdetail/mood_id/{$twitter_id}/user_id/{$sender_id}" target="_blank">评论</a>
			&nbsp;&nbsp;
			<a href="/tuita/viewdetail/mood_id/{$twitter_id}/user_id/{$sender_id}" target="_blank">转推</a>
			&nbsp;&nbsp;&nbsp;&nbsp;
			来自{$from}
		</div>
	</td>
</tr>
</table>

</table>
HTML;


		return '<div class="font14">' . $html . '</div>';

	} else {
		return '';
	}
}

